var latitude = '';
var longitude = '';
var geocoder;
var address;

function googleMaps(zoom,mark,title,descri){
    /*
    if(address){
        mark = 0;
    }
    */
    function initialize(){
        if(latitude.length == 0 && longitude.length == 0){
            // coordenadas de Maringá-PR
            latitude  = "-23.42100";
            longitude = "-51.93306";
        }else{
            address = null;
        }
        geocoder = new google.maps.Geocoder();
        var myCenter = new google.maps.LatLng(latitude,longitude);
        var mapProp = {
            center:myCenter,
            zoom:zoom,
            mapTypeId:google.maps.MapTypeId.ROADMAP
            };

        var map=new google.maps.Map(document.getElementById("googleMap"),mapProp);

        if(mark == 1){
            var marker=new google.maps.Marker({
                position:myCenter,
                title:title,
                map:map,
                //animation:google.maps.Animation.BOUNCE
                });

            marker.setMap(map);
            
            var infowindow = new google.maps.InfoWindow(), marker;
 
            google.maps.event.addListener(marker, 'click', (function(marker, i) {
                return function() {
                    infowindow.setContent(descri);
                    infowindow.open(map, marker);
                }
            })(marker))
        }
        
        
        
        
        if (geocoder && address) {
        geocoder.geocode( { 'address': address}, function(results, status) {
          if (status == google.maps.GeocoderStatus.OK) {
            if (status != google.maps.GeocoderStatus.ZERO_RESULTS) {
            map.setCenter(results[0].geometry.location);

              var infowindow = new google.maps.InfoWindow(
                  { content: '<b>'+address+'</b>',
                    size: new google.maps.Size(150,50)
                  });

              var marker = new google.maps.Marker({
                  position: results[0].geometry.location,
                  map: map, 
                  title:address
              }); 
              google.maps.event.addListener(marker, 'click', function() {
                  infowindow.open(map,marker);
              });

            } else {
              alert("No results found");
            }
          } else {
            alert("Geocode was not successful for the following reason: " + status);
          }
        });
      }
    }
    
    google.maps.event.addDomListener(window, 'load', initialize);
}

//google.maps.event.addDomListener(window, 'load', initialize);

function getLocation() {
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(showPosition);
    } else { 
        //alert("Geolocation is not supported by this browser.");
    }
}

function showPosition(position) {
    latitude  = position.coords.latitude;
    longitude = position.coords.longitude;

    document.getElementById("latitude").value  = position.coords.latitude;
    document.getElementById("longitude").value = position.coords.longitude;
}

//getLocation();

